using System;
using System.Collections.Generic;
using System.Text;
using VisualizationPackage.FileLoader;
using System.Collections.ObjectModel;
using System.Drawing;

namespace VisualizationPackage.VectorField
{
    public class StreamRibon
    {
        private List<Polygon> _path;
        public ReadOnlyCollection<Polygon> Path
        {
            get
            {
                if (_path != null)
                    return _path.AsReadOnly();
                return null;
            }
        }
        private StreamLine _streamLine1;
        public StreamLine StreamLine1
        {
            get { return _streamLine1; }
            set { _streamLine1 = value; }
        }
        private StreamLine _streamLine2;
        public StreamLine StreamLine2
        {
            get { return _streamLine2; }
            set { _streamLine2 = value; }
        }
        public static List<StreamRibon> StreamRibons = new List<StreamRibon>();

        public StreamRibon(StreamLine stLineStart)
        {
            _streamLine1 = stLineStart;
            stLineStart.Parent = this;
        }
        public ReadOnlyCollection<Polygon> Calculate()
        {
            if (Path != null)
                return Path;
            StreamLine2 = new StreamLine(StreamLine1.Start - new Vertex(2, 2, 2), StreamLine1.UVector, StreamLine1.VVector, StreamLine1.WVector, StreamLine1.DeltaT, StreamLine1.WorkingZone);
            StreamLine2.Calculate();
            StreamLine1.Calculate();
            List<Polygon> lstPolys = new List<Polygon>();
            int min = Math.Min(StreamLine1.Path.Count, StreamLine2.Path.Count);
            for (int i = 0; i < min; i++)
            {
                lstPolys.Add(CreatePolygon(StreamLine1.Path[i], StreamLine2.Path[i]));
            }
            return (_path = lstPolys).AsReadOnly();
        }

        private Polygon CreatePolygon(Line line, Line line_2)
        {
            double parameter = Math.Sqrt(12) / (line.Vertex1 - line_2.Vertex1).Magnitude();
            Vertex v1 = Vertex.InterpolatePosition(line.Vertex1, line_2.Vertex1, parameter);
            parameter = Math.Sqrt(12) / (line.Vertex2 - line_2.Vertex2).Magnitude();
            Vertex v2 = Vertex.InterpolatePosition(line.Vertex2, line_2.Vertex2, parameter);

            Polygon p = new Polygon();
            p.Add(line.Vertex1);
            p.Add(line.Vertex2);
            p.Add(v2);
            p.Add(v1);
            p.DrawingColor = Color.Black;
            return p;
        }
    }
}
